//
//  GHWithdrawViewController.m
//  GodHorses
//
//  Created by Mac on 2017/11/21.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "GHWithdrawViewController.h"
#import "GHCardMangeViewController.h"
#import "GHBankModel.h"
#define XInsetValeu 20
#define nomalHeight 44
#define bankeDes @"请选择银行卡"
@interface GHWithdrawViewController ()
@property(nonatomic, strong) STButton                     *withdrawTypeButton;//提现方式
@property(nonatomic, strong) STButton                     *bankButton;//银行
@property(nonatomic, strong) STLabel                     *yueLable;//余额
@property(nonatomic, strong) STLabel                     *yejiLable;//业绩
@property(nonatomic, strong) STLabel                     *qianbaoLable;//神马钱包，贷款
@property(nonatomic, strong) STLabel                     *shemaLable;//当前神马值

@property(nonatomic, strong) UITextField                     *zhihangTextFiled;
@property(nonatomic, strong) UITextField                     *nameTextFiled;
@property(nonatomic, strong) UITextField                     *accountTextFiled;
@property(nonatomic, strong) UITextField                     *moneyFiled;

@property(nonatomic, strong) GHUserModel                     *userModel;
@property(nonatomic, strong) NSArray                      *tixianArray;
@property(nonatomic, strong) NSArray                      *bankArray;
@property(nonatomic, strong) GHBankModel                     *bankModel;
@end

@implementation GHWithdrawViewController
- (void)setUserModel:(GHUserModel *)userModel{
    _userModel = userModel;

    self.yueLable.text = [NSString stringWithFormat:@"可提现金额￥%@",userModel.u_money];
    self.yueLable.attributedText = [self.yueLable.text st_convertAttributeStringWithKeyWord:@"可提现金额" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]}];
    
    self.yejiLable.text = [NSString stringWithFormat:@"可提现业绩￥%@",userModel.u_distribution];
    self.yejiLable.attributedText = [self.yejiLable.text st_convertAttributeStringWithKeyWord:@"可提现业绩" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]}];
    
    self.qianbaoLable.text = [NSString stringWithFormat:@"可提现神马钱包￥%@",userModel.payment];
    self.qianbaoLable.attributedText = [self.qianbaoLable.text st_convertAttributeStringWithKeyWord:@"可提现神马钱包" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]}];
    
    self.shemaLable.text = [NSString stringWithFormat:@"可提现当前神马值￥%@",userModel.now_wfpoints];
    self.shemaLable.attributedText = [self.shemaLable.text st_convertAttributeStringWithKeyWord:@"可提现当前神马值" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]}];
}
- (void)setBankModel:(GHBankModel *)bankModel{
    _bankModel = bankModel;
    [self.bankButton setTitle:bankModel.bank_name forState:UIControlStateNormal];
    self.zhihangTextFiled.text = bankModel.branch_name;
    self.nameTextFiled.text = bankModel.account_name;
    self.accountTextFiled.text = bankModel.account_bank;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"提现";
    [self configSubView];
    [self configFooterView];
    [self sendUserInfoRequest];
    [self sendBanksRequst];
    [self senddefultBankRequest];
    self.tixianArray = @[@"请选择提现类型",@"余额提现",@"业绩提现",@"神马钱包提现",@"当前神马值提现"];
    
}

#pragma mark --subView
- (void)configSubView{
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 300)];
    headerView.backgroundColor = [UIColor whiteColor];
    //提现方式按钮
    __weak typeof(self) weakSelf =  self;
    UIView *withdrawTypeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, nomalHeight)];
    withdrawTypeView.backgroundColor = self.tableView.backgroundColor;
    [headerView addSubview:withdrawTypeView];
    self.withdrawTypeButton = [[STButton alloc] initWithFrame:CGRectMake(XInsetValeu, 0, UIScreenWidth - XInsetValeu * 2, nomalHeight)
                                                     title:@"请选择提现类型"
                                                titleColor:GH_firstTextColor
                                                 titleFont:15
                                              cornerRadius:0
                                           backgroundColor:self.tableView.backgroundColor
                                           backgroundImage:nil
                                                     image:nil];
    [self.withdrawTypeButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedWithdrawTypeButton];
    }];
    [withdrawTypeView addSubview:self.withdrawTypeButton];
    
    //提现介绍
    UIImageView * yeallowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, withdrawTypeView.bottom, UIScreenWidth, 200)];
    yeallowImageView.backgroundColor = GH_YellowBackGroundColor;
    [headerView addSubview:yeallowImageView];
    self.yueLable = [[STLabel alloc] initWithFrame:CGRectMake(XInsetValeu, 10, UIScreenWidth - 2 * XInsetValeu, 20)
                                                     text:@"可提现金额￥"
                                                textColor:[UIColor whiteColor]
                                                     font:15
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    [yeallowImageView addSubview:self.yueLable];
    
    self.yejiLable = [[STLabel alloc] initWithFrame:CGRectMake(XInsetValeu,  _yueLable.bottom, UIScreenWidth - 2 * XInsetValeu, 20)
                                              text:@"可提现业绩￥"
                                         textColor:[UIColor whiteColor]
                                              font:15
                                       isSizetoFit:NO
                                     textAlignment:NSTextAlignmentCenter];
    [yeallowImageView addSubview:self.yejiLable];
    
    self.qianbaoLable = [[STLabel alloc] initWithFrame:CGRectMake(XInsetValeu,  _yejiLable.bottom, UIScreenWidth - 2 * XInsetValeu, 20)
                                              text:@"可提现神马钱包￥"
                                         textColor:[UIColor whiteColor]
                                              font:15
                                       isSizetoFit:NO
                                     textAlignment:NSTextAlignmentCenter];
    [yeallowImageView addSubview:self.qianbaoLable];
    
    self.shemaLable = [[STLabel alloc] initWithFrame:CGRectMake(XInsetValeu, _qianbaoLable.bottom, UIScreenWidth - 2 * XInsetValeu, 20)
                                              text:@"可提现当前神马值￥"
                                         textColor:[UIColor whiteColor]
                                              font:15
                                       isSizetoFit:NO
                                     textAlignment:NSTextAlignmentCenter];
    [yeallowImageView addSubview:self.shemaLable];
    yeallowImageView.height = self.shemaLable.bottom + 10;
    //收款银行卡
    UIView* bankView = [self headerViewWithTitle:@"收款银行卡"];
    bankView.top = yeallowImageView.bottom;
    [headerView addSubview:bankView];
    self.bankButton = [[STButton alloc] initWithFrame:CGRectMake(0, bankView.bottom, UIScreenWidth, nomalHeight)
                                                     title:bankeDes
                                                titleColor:GH_firstTextColor
                                                 titleFont:14
                                              cornerRadius:0
                                           backgroundColor:[UIColor whiteColor]
                                           backgroundImage:nil
                                                     image:nil];
    [self.bankButton setTitleEdgeInsets:UIEdgeInsetsMake(0, XInsetValeu, 0, 0   )];
    [self.bankButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedBankTypeButton];
    }];
    [headerView addSubview:self.bankButton];
    //支行
    UIView * zhihangView = [self headerViewWithTitle:@"支行地址"];
    zhihangView.top = self.bankButton.bottom;
    [headerView addSubview:zhihangView];
    self.zhihangTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(XInsetValeu, zhihangView.bottom, UIScreenWidth - 2* XInsetValeu, nomalHeight)];
    self.zhihangTextFiled.placeholder = @"xx省xx市xx区xx支行";
    self.zhihangTextFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
    [headerView addSubview:self.zhihangTextFiled];
    //姓名
    UIView * xingmingView = [self headerViewWithTitle:@"姓名"];
    xingmingView.top = self.zhihangTextFiled.bottom;
    [headerView addSubview:xingmingView];
    self.nameTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(XInsetValeu, xingmingView.bottom, UIScreenWidth - 2* XInsetValeu, nomalHeight)];
    self.nameTextFiled.placeholder = @"请输入真实姓名";
     self.nameTextFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
    [headerView addSubview:self.nameTextFiled];
    //账号
    UIView * accountView = [self headerViewWithTitle:@"账号"];
    accountView.top = self.nameTextFiled.bottom;
    [headerView addSubview:accountView];
    self.accountTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(XInsetValeu, accountView.bottom, UIScreenWidth - 2* XInsetValeu, nomalHeight)];
    self.accountTextFiled .placeholder = @"请输入银行卡卡号";
     self.accountTextFiled.keyboardType = UIKeyboardTypeNumberPad;
    self.accountTextFiled .clearButtonMode = UITextFieldViewModeWhileEditing;
    [headerView addSubview:self.accountTextFiled ];
    
    //银行卡
    UIView * moneyView = [self headerViewWithTitle:@"提现金额"];
    moneyView.top  = self.accountTextFiled.bottom;
    [headerView addSubview:moneyView];
    self.moneyFiled = [[UITextField alloc] initWithFrame:CGRectMake(XInsetValeu, moneyView.bottom, UIScreenWidth - 2* XInsetValeu, nomalHeight)];
    self.moneyFiled .placeholder = @"单笔提现金额：100整数倍，最高10000元";
    self.moneyFiled .clearButtonMode = UITextFieldViewModeWhileEditing;
    self.moneyFiled.keyboardType = UIKeyboardTypeNumberPad;
    [headerView addSubview:self.moneyFiled ];
    
    headerView.height = self.moneyFiled.bottom;
    self.tableView.tableHeaderView = headerView;
}
- (void)configFooterView{
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = self.tableView.backgroundColor;
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, 0, UIScreenWidth - 60, 44)
                                                        title:@"提交"
                                                   titleColor:GH_YellowBackGroundColor
                                                    titleFont:18
                                                 cornerRadius:5
                                              backgroundColor:GH_BlackBackGroundColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedCommitButton];
    }];
    confimButton.st_centerY = 50;
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:confimButton];
    
    STButton * addButton = [[STButton alloc] initWithFrame:CGRectMake(30, confimButton.bottom + 44, UIScreenWidth - 60, 44)
                                                        title:@"绑定银行卡"
                                                   titleColor:GH_YellowBackGroundColor
                                                    titleFont:18
                                                 cornerRadius:5
                                              backgroundColor:GH_BlackBackGroundColor
                                              backgroundImage:nil
                                                        image:nil];
    [addButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedAddBankButton];
    }];
    addButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:addButton];
    
    footer.height = addButton.bottom + 50;
    self.tableView.tableFooterView = footer;
}
- (UIView*)headerViewWithTitle:(NSString*)title{
    UIView * backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, nomalHeight)];
    backView.backgroundColor = self.tableView.backgroundColor;
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(XInsetValeu, 0, UIScreenWidth - 2* XInsetValeu, nomalHeight)
                                                     text:title
                                                textColor:GH_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [backView addSubview:titleLable];
    return backView;
}

#pragma mark --Action Method
- (void)onSelectedWithdrawTypeButton{
    
    STPickerViewController * vc = [[STPickerViewController alloc] initWithPickerArray:@[self.tixianArray] andWithHandle:^(NSArray<NSString *> *stringArray) {
        [self.withdrawTypeButton setTitle:stringArray.firstObject forState:UIControlStateNormal];
    }];
    [self presentViewController:vc animated:NO completion:nil];
}
- (void)onSelectedBankTypeButton{
    if (!self.bankArray.count) {
        [self sendBanksRequst];
        return;
    }
    STPickerViewController * vc = [[STPickerViewController alloc] initWithPickerArray:@[self.bankArray] andWithHandle:^(NSArray<NSString *> *stringArray) {
        [self.bankButton setTitle:stringArray.firstObject forState:UIControlStateNormal];
    }];
    [self presentViewController:vc animated:NO completion:nil];
}
- (void)onSelectedCommitButton{
    //提交判断
    NSInteger typeNum = [self.tixianArray indexOfObject:self.withdrawTypeButton.currentTitle];
    if (!typeNum) {
        [SVProgressHUD showErrorWithStatus:@"请选择提现类型"];
        return;
    }
    if ([self.bankButton.currentTitle isEqualToString:bankeDes]) {
        [SVProgressHUD showErrorWithStatus:bankeDes];
        return;
    }
    if (!self.zhihangTextFiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入支行地址"];
        return;
    }
    if (!self.nameTextFiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入姓名"];
        return;
    }
    if (!self.accountTextFiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入卡号"];
        return;
    }
    if (!self.moneyFiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入提现金额"];
        return;
    }
    [self sendTixianRequest];
}
- (void)onSelectedAddBankButton{
    [self.navigationController pushViewController:[GHCardMangeViewController new] animated:YES];
}
#pragma mark --NetWork Method
//获取余额
- (void)sendUserInfoRequest{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/index/personal"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 NSDictionary * data = [responseObject valueForKey:@"data"];
                                                 if ([[data allKeys] containsObject:@"user_info"]) {
                                                     NSDictionary * user_info = [data valueForKey:@"user_info"];
                                                     GHUserModel * user = [GHUserModel mj_objectWithKeyValues:user_info];
                                                     self.userModel = user;
                                                 }
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 NSLog(@"error:%@",error.desc);
                                             }];
}
//获取银行列表
- (void)sendBanksRequst{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/cash/get_banks"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 NSDictionary * dataArray = [responseObject valueForKey:@"data"];
                                                 NSMutableArray *newarrya = [NSMutableArray new];
                                                 [newarrya addObject:bankeDes];
                                                 for (NSDictionary * dataDic in dataArray) {
                                                     if ([dataDic.allKeys containsObject:@"bank"]) {
                                                         NSString * bank = dataDic[@"bank"];
                                                         [newarrya addObject:bank];
                                                     }
                                                 }
                                                 self.bankArray = newarrya;
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 NSLog(@"error:%@",error.desc);
                                             }];
}
//提现
- (void)sendTixianRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    NSInteger typeNum = [self.tixianArray indexOfObject:self.withdrawTypeButton.currentTitle];
    [dic setObject:@(typeNum) forKey:@"type"];
    
    [dic setObject:self.bankButton.currentTitle forKey:@"bank_name"];
    if (self.zhihangTextFiled.text.length) {
        [dic setObject:self.zhihangTextFiled.text forKey:@"branch_name"];
    }
    if (self.nameTextFiled.text.length) {
        [dic setObject:self.nameTextFiled.text forKey:@"name"];
    }
    if (self.accountTextFiled.text.length) {
        [dic setObject:self.accountTextFiled.text forKey:@"account"];
    }
    if (self.moneyFiled.text.length) {
        [dic setObject:self.moneyFiled.text forKey:@"cash"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/cash/do_withdraw"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                  [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"提现成功"];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];

}
//获取默认银行卡
- (void)senddefultBankRequest{

    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/cash/get_default_addr"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                  [SVProgressHUD dismiss];
                                                 NSDictionary *dataDic = responseObject[@"data"];
                                                 self.bankModel = [GHBankModel mj_objectWithKeyValues:dataDic];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                // [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end
